<?php
class programme_subscription extends Model
{
    public static function getTableName()
    {
        return 'programme_subscription';
    }

    public function getPrimaryKey()
    {
        return array('programme_id');
    }

    public function set($obj)
    {
        $this->programme_id = isset($obj->id) && !is_null($obj->id) ? $obj->id : ''; 
        /* bug 5457
        if ('allow' == $obj->subscription_country_allow_deny) {
            $this->country_allow_list = is_null($obj->subscription_country) ? '' :  ParseCountryCode::json2country($obj->subscription_country);
            $this->country_deny_list = 'ALL';
        } else {
            $this->country_allow_list = 'ALL';
            $this->country_deny_list = is_null($obj->subscription_country) ? '' :  ParseCountryCode::json2country($obj->subscription_country);
        }
         */
        $this->country_allow_list = AC::DEFAULT_COUNTRY_ALLOW_LIST;
        $this->country_deny_list = AC::DEFAULT_COUNTRY_DENY_LIST;

        $this->free_flag = isset($obj->subscription_free) ? $obj->subscription_free  : 0; 
        $this->publish_datetime = isset($obj->subscription_publish_datetime) ? $obj->subscription_publish_datetime  : ''; 
        $this->expire_datetime = isset($obj->subscription_expire_datetime) ? $obj->subscription_expire_datetime  : ''; 
    }


    public function get($id)
    {
        if($id)
        {
            $data = self::$dbo['r']->findInstance($this->getTableName(),$id);
        }else
        {
            $data =  self::$dbo['r']->find($this->getTableName());
        }
        if($data)
        {
            if('ALL' == $data->country_deny_list)
            {
                $data->country_allow_list = ParseCountryCode::country2json(AC::COUNTRY_LIST_CODE,$data->country_allow_list,0);
            }
            else
            {
                $data->country_deny_list = ParseCountryCode::country2json(AC::COUNTRY_LIST_CODE,$data->country_deny_list,1);
            }
        }
        else
        {
            $data = '';
        }

        return $data;
    }

    public function update($obj)
    {
        return Model::$dbo['w']->save($obj);
    }
    
    public function delete($id)
    {
        return Model::$dbo['w']->delete('programme_subscription','programme_id=?',array($id));
    }
}
?>
